<?php
// +----------------------------------------------------------------------
// | Scallion 
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.scallion.top All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Liuxg < 420695648@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class RecycleController extends AdminBaseController
{

    /**
     * 回收站管理
     */
    public function index()
    {
		$data = db('data_recycle')->order('id desc')->paginate(10);
		if($data){
			$this->assign('page', $data->render());
			$data = json_encode($data);
			$data = json_decode($data,true);
			for($i=0;$i<count($data['data']);$i++){
				if($data['data'][$i]['table_name'] == 'teacher'){
					$temp = json_decode($data['data'][$i]['json_data'],true);
					unset($temp['introduction']);
					unset($temp['note']);
					$data['data'][$i]['json_data'] = json_encode($temp);
				}
			}
		}
		$this->assign("data", $data['data']);
			// dump($data);
        return $this->fetch();
    }
	
	//还原操作
	public function recycle_restore(){
		if($this->request->param()){
			$param = $this->request->param();
			$info = db('data_recycle')->where('id='.$param['id'])->find();
			if($info){
				$data = json_decode($info['json_data'],true);
				if($is_exist = db($info['table_name'])->where('id='.$data['id'])->find()){$this->error('数据冲突！不能还原！');}
				$res = db($info['table_name'])->insert($data);
				if($res){
					
					//填写日志
					$action = '还原信息至表：'.config('database.prifix').$info['table_name'];
					$json_data = $info['json_data'];
					$result = '成功';
					self::data_log($action,$json_data,$result);
					
					db('data_recycle')->where('id='.$param['id'])->delete();
					$this->success('数据还原成功！',url('Recycle/index'));
				}
				else{
					$this->error('服务器错误！-202');
				}
			}
			else{
				$this->error('数据不存在！-201');
			}
		}
		else{
			$this->error('非法访问！');
		}
	}
}
